--
-- Craft one shear using the other one and back
--

local modname = minetest.get_current_modname()
-- local modpath = minetest.get_modpath(modname)

-- Load support for MT game translation.
local S = minetest.get_translator(modname)

-- Shears items
local vines_shears = minetest.registered_items["vines:shears"]
local mobs_shears = minetest.registered_items["mobs:shears"]

local mod_mobs = minetest.get_modpath('mobs')
local mod_vines = minetest.get_modpath('vines')

local mobs_sheep = minetest.registered_entities['mobs:sheep']

-- Descriptions and usage help
local strings = { 
    shears_description = S("Shears"),
    rightclick_to_shear = S("Some tamed animals can be sheared using right-click."),
    cuts_wool_and_snappy = S("A very useful tool, ideal to cut strings or collect wool. Also works very well on small plants, leaves and vines."),
    dig_to_harvest_vines = S("Cut vines with these shears to harvest them."),
    }

local inv_image = "creatures_shears.png"

--
-- CRAFT ONE SHEARS WITH THE OTHER
--
-- if vines_shears and mobs_shears then
  -- minetest.register_craft({
    -- type = "shapeless",
    -- output = "mobs:shears",
    -- recipe = {"vines:shears"}
  -- })
  -- minetest.register_craft({
    -- type = "shapeless",
    -- output = "vines:shears",
    -- recipe = {"mobs:shears"}
  -- })
-- end

-- local vines_types = { 'root', 'vine', 'side', 'jungle', 'willow' }
-- local sheep_colors = {"black", "blue", "brown", "cyan", "dark_green", "dark_grey", "green", "grey", "magenta", "orange", "pink", "red", "violet", "white", "yellow" }

-- Shears definition based on vines:shears
local shears = {
      description = strings.shears_description,
      inventory_image = inv_image,
      wield_image = inv_image,
      stack_max = 1,
      max_drop_level=3,
      tool_capabilities = {
        full_punch_interval = 1.0,
        max_drop_level=0,
        groupcaps={
          snappy={times={[3]=0.2}, uses=60, maxlevel=3},
          wool={times={[3]=0.2}, uses=60, maxlevel=3}
        }
      }
    }

if vines_shears then

  -- Use original mod values
  shears.tool_capabilities = vines_shears.tool_capabilities
  shears.max_drop_level = vines_shears.max_drop_level
  shears.stack_max = vines_shears.stack_max
  shears.wield_image = inv_image
  shears.inventory_image = inv_image
  
end

if mobs_sheep then
  -- Use original mod values
  shears._doc_items_longdesc = strings.cuts_wool_and_snappy
  shears._doc_items_usagehelp =  ''
        .. strings.dig_to_harvest_vines 
        ..' '
        .. strings.rightclick_to_shear
else

  shears._doc_items_longdesc = strings.cuts_wool_and_snappy
  shears._doc_items_usagehelp = strings.dig_to_harvest_vines 

end

--
-- IF VINES ONLY
--
if vines_shears and not mobs_shears then
  minetest.override_item("vines:shears", shears)
  minetest.register_alias_force("mobs:shears", "vines:shears")  
end


--
-- IF MOBS
--
if mobs_shears then

  minetest.override_item("mobs:shears", shears)
  --
  -- IF VINES AND MOBS MOD 
  --
  if mod_vines and vines_shears then

    -- Register alias so that mobs:shears is used every time vines:shears would 
    minetest.unregister_item('vines:shears')
    minetest.register_alias_force("vines:shears", "mobs:shears")  

    -- Overrides the default vines.dig function
    if vines.register_vine and vines.dig_vine then
      vines.dig_vine = function( pos, node_name, user )
        --only dig give the vine if shears are used
        if not user then return false end
        local wielded = user:get_wielded_item()
        if 'mobs:shears' == wielded:get_name() then
          local inv = user:get_inventory()
          if inv then
            inv:add_item("main", ItemStack( node_name ))
          end
        end
      end
    end

  end
end


